home *** CD-ROM | disk | FTP | other *** search
- // phong_fragment.cg
- //
- // This file cannot be directly compiled through CG... it first must be
- // parsed to insert channel-specific instructions.
- //
- // For example:
- // cgc.exe "$(InputPath)" -profile fp30 -o $(InputName).fp -DSPOT_LIGHT -DTRANSP_IN_TRANSP_RGB -DPROJ_LIGHT_TEXTURE
-
- struct vert2frag
- {
- float4 hPosition : POSITION;
-
- #if defined(BUMP_MAP)
- float3 wBinormal : TEXCOORD0;
- float3 wTangent : TEXCOORD1;
- float4 bumpTexUv : TEXCOORD2;
- #else
- float3 wNormal : TEXCOORD0;
- #endif // BUMP_MAP
-
- float4 transpTexUv : TEXCOORD3;
- float4 incanTexUv : TEXCOORD4;
- float4 reflectivityTexUv : TEXCOORD5;
- float4 wPosition : TEXCOORD6;
- };
-
- float4 main(vert2frag IN,
- uniform float4 wEyePosition,
- uniform float normalMultiplier,
-
- #if defined(BUMP_MAP)
- uniform sampler2D bumpTex,
- uniform float4 bumpTexMatrix0,
- uniform float4 bumpTexMatrix1,
- #endif // BUMP_MAP
-
- //[claforte] May one day be used to mimic software.
- //uniform sampler2D specTex,
- //uniform float4 specTexMatrix0,
- //uniform float4 specTexMatrix1,
-
- uniform sampler2D transpTex,
- uniform float4 transpTexMatrix0,
- uniform float4 transpTexMatrix1,
-
- uniform sampler2D incanTex,
- uniform float4 incanTexMatrix0,
- uniform float4 incanTexMatrix1,
-
- #if defined(REFLECT_CUBE_LOOKUP) || defined(REFRACT_CUBE_LOOKUP)
- uniform float3 worldToCubeRotationMatrix0,
- uniform float3 worldToCubeRotationMatrix1,
- uniform float3 worldToCubeRotationMatrix2,
- uniform samplerCUBE reflectedColTex,
- #endif // (REFLECT_CUBE_LOOKUP) || (REFRACT_CUBE_LOOKUP)
-
- #if defined(REFLECT_SPHERE_LOOKUP)
- uniform sampler2D reflectedColTex,
- #endif
-
- #if defined (REFRACT_CUBE_LOOKUP)
- // Refractive indices for red, green, blue.
- uniform float3 refractiveIndices,
- #endif
-
- uniform sampler2D reflectivityTex,
- uniform float4 reflectivityTexMatrix0,
- uniform float4 reflectivityTexMatrix1) : COLOR
- {
- // Calculate view direction in world space,
- // from point to eye.
- //
- float3 wViewDir = normalize(wEyePosition.xyz - IN.wPosition.xyz);
-
- #if defined(BUMP_MAP)
- // Compute an orthonomal basis to transform from tangent space
- // to world space, and vice-versa.
- //
- float3 wTangent = normalize(IN.wTangent);
- float3 wNormal = normalize(cross(wTangent, IN.wBinormal));
- float3 wBinormal = cross(wNormal, wTangent);
-
- // Translate and scale bump texture coordinates, by multiplying
- // the uvs by the bump texture matrix.
- //
- float2 placedBumpTexUv;
- float4 bumpTexUv = float4(IN.bumpTexUv.xy, 0.0, 1.0);
- placedBumpTexUv.x = dot(bumpTexMatrix0, bumpTexUv);
- placedBumpTexUv.y = dot(bumpTexMatrix1, bumpTexUv);
-
- // Fetch the bumped normal (in tangent space) from the bump texture.
- // The (... * 2 - 1) at the end is used to unpack the normal components,
- // from the [0,1] to [-1, -1] range.
- //
- float3 tBumpedNormal = normalize(f3tex2D(bumpTex, placedBumpTexUv) * 2 - 1);
-
- // Compute the world-space bumped normal.
- //
- float3 wBumpedNormal = normalMultiplier * tBumpedNormal.x * wTangent +
- normalMultiplier * tBumpedNormal.y * wBinormal +
- tBumpedNormal.z * wNormal;
- #else // !defined(BUMP_MAP)
- float3 wNormal = normalize(IN.wNormal);
- float3 wBumpedNormal = wNormal;
- #endif // BUMP_MAP
-
-
- // To support transparency and back-face lighting,
- // allow normal inversion.
-
- wNormal = normalMultiplier * wNormal;
- wBumpedNormal = normalMultiplier * wBumpedNormal;
-
- // Compute the viewer's reflection direction.
- //
- float3 wReflectDir = reflect(-wViewDir, wBumpedNormal);
-
-
- #if defined(REFLECT_CUBE_LOOKUP)
-
- // Transform the reflection vector from world space to
- // cube space. Ideally, we'd take the full cube placement
- // information into account, but since this is quite
- // complicated, we use the traditional OpenGL-shortcut of
- // only applying a rotation.
- //
- float3 cReflectDir;
- cReflectDir.x = dot(wReflectDir, worldToCubeRotationMatrix0);
- cReflectDir.y = dot(wReflectDir, worldToCubeRotationMatrix1);
- cReflectDir.z = dot(wReflectDir, worldToCubeRotationMatrix2);
- float3 reflectedColor = f3texCUBE(reflectedColTex, cReflectDir);
-
- #elif defined(REFLECT_SPHERE_LOOKUP)
-
- // This calculation is directly taken from the OpenGL spec's sphere
- // mapping section.
- //
- float x = wReflectDir.x;
- float y = wReflectDir.y;
- float z = wReflectDir.z + 1;
- float m = 2.0 * sqrt(x * x + y * y + z * z);
-
- float2 sphereUv = wReflectDir.xy / m + 0.5;
- float3 reflectedColor = f3tex2D(reflectedColTex, sphereUv);
-
- #else
-
- // Set the reflected color to black.
- float3 reflectedColor = float3(0, 0, 0);
-
- #endif
-
- // Apply the reflectivity texture matrix to the corresponding texture
- // coordinates, then fetch the appropriate texel.
- //
- float2 placedReflectivityTexUv;
- float4 reflectivityTexUv = float4(IN.reflectivityTexUv.xy, 0.0, 1.0);
- placedReflectivityTexUv.x = dot(reflectivityTexMatrix0, reflectivityTexUv);
- placedReflectivityTexUv.y = dot(reflectivityTexMatrix1, reflectivityTexUv);
- float reflectivity = f4tex2D(reflectivityTex, placedReflectivityTexUv).r;
-
- // Apply the incandescence texture matrix to the corresponding texture
- // coordinates, then fetch the appropriate texel.
- //
- float2 placedIncanTexUv;
- float4 incanTexUv = float4(IN.incanTexUv.xy, 0.0, 1.0);
- placedIncanTexUv.x = dot(incanTexMatrix0, incanTexUv);
- placedIncanTexUv.y = dot(incanTexMatrix1, incanTexUv);
- float3 incandescence = f3tex2D(incanTex, placedIncanTexUv);
-
- float3 refractedColor = 0;
-
- #if defined(REFRACT_CUBE_LOOKUP)
-
- float4 opacityColor;
-
- #if defined(OPACITY_IN_COLOR_ALPHA)
- // The opacity is encoded in the base color texture's alpha.
- // (NOTE: the cpp code is expected to bind the color_tex texture
- // to the transpTex sampler2D.)
- //
- opacityColor = fetchedTranspColor.aaaa;
-
- #else // TRANSP_IN_TRANSP_RGB
- // Apply the transparency texture matrix to the corresponding texture
- // coordinates, then fetch the appropriate texel.
- //
- float2 placedTranspTexUv;
- float4 transpTexUv = float4(IN.transpTexUv.xy, 0.0, 1.0);
- placedTranspTexUv.x = dot(transpTexMatrix0, transpTexUv);
- placedTranspTexUv.y = dot(transpTexMatrix1, transpTexUv);
- float4 fetchedTranspColor = f4tex2D(transpTex, placedTranspTexUv);
-
- // The transparency color is encoded in the transparency texture's RGB,
- // and the opacity is stored in the transparency texture's alpha.
- //
- opacityColor.rgb = 1-fetchedTranspColor.rgb;
- opacityColor.a = fetchedTranspColor.a;
- #endif //
-
- // Compute the refracted color.
- float3 cViewDir;
- cViewDir.x = dot(-wViewDir, worldToCubeRotationMatrix0);
- cViewDir.y = dot(-wViewDir, worldToCubeRotationMatrix1);
- cViewDir.z = dot(-wViewDir, worldToCubeRotationMatrix2);
-
- float3 cBumpedNormal;
- cBumpedNormal.x = dot(wBumpedNormal, worldToCubeRotationMatrix0);
- cBumpedNormal.y = dot(wBumpedNormal, worldToCubeRotationMatrix1);
- cBumpedNormal.z = dot(wBumpedNormal, worldToCubeRotationMatrix2);
-
- // TODO: implement and test chromatic abherration.
- //
- float3 cRefractDir = refract(cViewDir, cBumpedNormal, refractiveIndices.r);
- refractedColor = f3texCUBE(reflectedColTex, cRefractDir);
-
- // Modulate the refracted color by the transparency.
- // (ie: a highly transparent surface will translate into a highly
- // refractive surface.)
- //
- refractedColor.rgb = refractedColor * (1-opacityColor.rgb);
-
- #endif // REFRACT_CUBE_LOOKUP
-
- float4 finalColor;
- finalColor.rgb = reflectivity * reflectedColor + incandescence + refractedColor;
- finalColor.a = 1;
-
- return finalColor;
- }
-